package com.akaovi.chatroom.utils;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @author akaovi
 * @version 1.0
 * @description
 * @date 2022/12/7 16:43
 * @since 2022/12/7 16:43
 */
public class CommonUtils {
    /**
     * 用于统一关闭资源
     *
     * @param socket socket连接
     * @param oos    对象输出流
     * @param ois    对象输入流
     */
    public static void closeResource(Socket socket, ObjectOutputStream oos, ObjectInputStream ois) {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断一个字符串是否为空
     *
     * @param string a String
     * @return boolean
     */
    public static boolean isEmpty(String string) {
        if (string == null || string.length() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 有一个为空则为 true
     *
     * @param strings some strings
     * @return boolean
     */
    public static boolean isAnyEmpty(String... strings) {
        for (String string : strings) {
            if (isEmpty(string)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 所有字符串为空则为 true
     *
     * @param strings some strings
     * @return boolean
     */
    public static boolean isAllEmpty(String... strings) {
        for (String string : strings) {
            if (!isEmpty(string)) {
                return false;
            }
        }
        return true;
    }

    /**
     * md5 加密算法
     *
     * @param s 待加密文本
     * @return 加密后的文本
     */
    public static String getMd5(String s) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            byte[] messageDigest = md.digest(s.getBytes());

            BigInteger no = new BigInteger(1, messageDigest);

            String hashtext = no.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String generateAccount() {
        StringBuilder str = new StringBuilder();//定义变长字符串
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < 8; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }

    public static void main(String[] args) {
        System.out.println(generateAccount());
    }

}
